package com.token.sysflow.action;

import com.alibaba.fastjson.JSON;
import com.token.base.FuncUtil;
import com.token.base.Pl;
import com.token.sql.IntegrateSql;
import com.token.sysflow.impl.AbstractFlowHandlerImpl;
import com.token.sysflow.impl.SubmitFlowImpl;

/**
 * 提交流程
 * @author chenjianxiong 2020/4/12
 */
public class SubmitFlowAction extends AbstractFlowAction {
    @Override
    protected void publicParameter(FuncUtil f, AbstractFlowHandlerImpl handler) throws Exception {
        //操作时间
        handler.setActionTimer(Pl.getCurDataTime());

        //操作用户
        handler.setActionUser(f.getEffectiveUserName());

        //租户Id
        handler.setSysId(Pl.getSysId());
    }

    public void executeAction(FuncUtil f, IntegrateSql iSql) throws Exception{
        SubmitFlowImpl handler = new SubmitFlowImpl();
        String keyUnid = f.getUrlQueryString("keyUnid"); //主流程的keyUnid
//        System.out.println("--->SubmitFlowAction.executeAction = " + keyUnid) ;
        //起草第一次提交时
        if(keyUnid == null || "".equals(keyUnid)){
            handler.setFlowId(f.getUrlQueryString("flowId", true, "流程Id[flowId]"));
            handler.setAppKeyId(f.getUrlQueryString("appKeyId",true,"应用Id[appKeyId]"));
            handler.setApplyer(f.getUrlQueryString("applyer",true,"申请人[applyer]"));
            handler.setCreater(f.getUrlQueryString("creater",true,"创建人[creater]"));
            handler.setFirstSubmit(true);

        }else{
            handler.setKeyUnid(keyUnid);
            handler.setFirstSubmit(false);
            handler.setSubKeyUnid(f.getUrlQueryString("subKeyUnid")); //子流程
        }

        //办理人，会签会审不用传selectApprovers
        String approvers = f.getUrlQueryString("selectApprovers");
        if(approvers != null && !"".equals(approvers)){
            handler.setSelectApprover(approvers.split(";"));
        }

        //指定目标环节Id，会签会审不用传stepId
        String stepId = f.getUrlQueryString("stepId");
        if(stepId != null && !"".equals(stepId)){
            handler.setStepId(stepId);
        }

        //办理意见
        handler.setOpinions(f.getUrlQueryString("opinions"));

        //会审传入
        String hs = f.getUrlQueryString("result");
        if(hs != null && !hs.equals("")){
            handler.setHSResult(hs.equals("1")?SubmitFlowImpl.HSResult_YES:SubmitFlowImpl.HSResult_NO);
        }

        //表单字段的值传入。
        handler.setFieldValues(JSON.parseObject(f.getUrlQueryString("fieldValues")));

        this.start(f,iSql,handler);
    }
}
